import { LEGO_API_HOST, MOS_API_HOST,stgEnv  } from './config/default/index'
const TerserPlugin = require('terser-webpack-plugin')

const notProd = process.env.APP_ENV !== 'pro'
import { logError } from './util/log4j'
let plugins = [
  { src: '@/plugins/log.js', mode: 'server' },
  { src: '@plugins/inject/URLS.js' },
  { src: '@plugins/inject/sendQdas.js' },
  { src: '@plugins/axios' }
]
if (['stg1','stg2','stg3','local'].includes(stgEnv)){
  // 测试环境需要加上的插件
  plugins.push( { src: '@/plugins/vconsole.js',mode: 'client' })
}
export default {
  vue: {
    config: {
      productionTip: notProd,
      devtools: notProd
    }
  },
  loading: false,
  env:{

  },
  head: {
    title: '',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1.0,user-scalable=0,maximum-scale=1.0' },
      { hid: 'description', name: 'description', content: '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
    ],
    script: [
      // 后面要换掉
    ],
  },
  router: {
    middleware: ['judgeFacility', 'setCookie']
  },
  hooks: {
    render: {
      errorMiddleware(app) {
        app.use((error, _req, _res, next) => {
          if (error) {
            console.log(error)
            logError(`【Node端错误】${error}`)
          }
          next(error)
        })
      },
    },
  },
  // Global CSS
  // 后面要换掉
  css: [
  ],
  plugins: plugins,
  buildModules: [
  ],
  modules: [
    '@nuxtjs/axios'
  ],
  axios: {
    browserBaseURL:LEGO_API_HOST,
    baseURL:LEGO_API_HOST,
    proxy: true,
    credentials: true
  },
  proxy: {
    // 后面要换掉
    '/mos-api/': {
      target: `${MOS_API_HOST}/`,
      changeOrigin: true,
      pathRewrite: {
        '^/mos-api/': ''
      }
    }
  },
  build: {
    filenames:{
      // 配置打包的图片文件名为hash
      img: '[path][contenthash:7].[ext]',
    },
    // 分析JS包的大小
    // analyze: true,
    optimization:{
      minimizer:[new TerserPlugin({
        terserOptions: {
          compress: {
            drop_console:(stgEnv==='pro'?true:false)
          },
        },
      })],
      splitChunks: {
        chunks: 'all',
        automaticNameDelimiter: '.',
        name: true,
          
        cacheGroups: {
          vendors: {
            test: /[\\/]node_modules[\\/]/,
            priority: -10
          },
          default: {
            minChunks: 2,
            priority: -20,
            reuseExistingChunk: true
          },
          elementUI: {
            name:'chunk-elementUI',
            priority:22,
            test:/[\\/]node_modules[\\/]_?element-ui(.*)/
          },
          echarts: {
            name:'chunk-echarts',
            priority:22,
            test:/[\\/]node_modules[\\/]_?echarts(.*)/
          },
          html2canvas: {
            name:'chunk-html2canvas',
            priority:22,
            test:/[\\/]node_modules[\\/]_?html2canvas(.*)/
          },
          moment: {
            name:'chunk-moment',
            priority:22,
            test:/[\\/]node_modules[\\/]_?moment(.*)/
          },
          vconsole: {
            name:'chunk-vconsole',
            priority:21,
            test:/[\\/]node_modules[\\/]_?vconsole(.*)/
          },
          jquery: {
            name:'chunk-jquery',
            priority:20,
            test:/[\\/]node_modules[\\/]_?jquery(.*)/
          }
        }
      }
    },
    // CSS 单独抽取成文件，否则会打包到JS中影响体验
    extractCSS: { allChunks: true, ignoreOrder: true },
    // 添加这个是关键，添加后babel才会处理依赖包vant里面的代码
    transpile: [/vant.*?less/],
    extend (config, ctx) {
      if (ctx.isClient) {
        // 添加 alias 配置
        // config.resolve.alias['~src'] = __dirname
        // config.resolve.alias['~utils'] = path.join(__dirname, 'utils')
      }
    },
    babel: {
      plugins: [
        ['import', {
          libraryName: 'vant',
          'style': (name) => {
            return `${name}/style/less.js`
          }
        }, 'vant']
      ],
    },
    postcss: {
      plugins: {
        'autoprefixer': {},
        'postcss-pxtorem': {
          'rootValue': 50,
          'propList': [
            '*'
          ]
        }
      }
    }
  }
}
